from langchain_community.chat_message_histories import ChatMessageHistory
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder, SystemMessagePromptTemplate
from langchain_ollama import ChatOllama

chat_history = ChatMessageHistory()

prompt = ChatPromptTemplate.from_messages(
    [
        SystemMessagePromptTemplate.from_template("只需要给出相关的答案即可，不要再加任何多余的内容。"),
        MessagesPlaceholder(variable_name="messages")
    ]
)

# 将用户的第一个输入添加到chat_history中
input1 = "将这句话翻译成英文：我喜欢AI大模型。"
chat_history.add_user_message(input1)

llm = ChatOllama(
    model="llama3",
    temperature=0.5,
)

chain = prompt | llm

response1 = chain.invoke({
    "messages": chat_history.messages
})

# 将ai的回复添加到chat_history中
chat_history.add_ai_message(response1.content)

# 将用户的第二个输入添加到chat_history中
input2 = "你刚刚说什么？"
chat_history.add_user_message(input2)

response2 = chain.invoke({
    "messages": chat_history.messages
})

print(response2.content)
